let luoke = require("../../utils/luoke");
Page({
    data: {
       uName: "",

       phone: "",
       region: [],
       detailAddress: "",
       rightIcon: "/public/icon/chevron_right.png"
    },
    onShow () {
        let userInfo = wx.getStorageSync("userInfo");
        if (userInfo) {
            luoke.outTimeFn()
        }   
        let resiveGoodInfo = wx.getStorageSync('resiveGoodInfo');
        if (!resiveGoodInfo) {
          return ;
        }
        this.setData({
            uName: resiveGoodInfo.uName,
            
            phone: resiveGoodInfo.phone,   
            region: resiveGoodInfo.region,
            detailAddress: resiveGoodInfo.detailAddress
        })
    },
    putUserNameFn: function (e) {
        let uName = e.detail.value;
        this.setData({
            uName: uName
        })
    },
    putPhoneFn: function (e) {
         let phone =  e.detail.value;
         this.setData({
            phone: phone
         })
    },
    detailFn: function (e) {
        let detailAddress = e.detail.value;
        this.setData({
            detailAddress: detailAddress
        
        
        })
    },
    bindRegionChange: function (e) {
        this.setData({
          region: e.detail.value
        })
    },
    showErrorFn: function (errorMsg, errorStatus) {
        wx.showToast({
          title: errorMsg,
          icon:  errorStatus || "error",
          mask: true
      })
    },
    submitFn: function ( ) {
        let _this = this;
        
        let uName = this.data.uName;
        let phone = this.data.phone;
        let region = this.data.region;      
        let detailAddress = this.data.detailAddress;
        let reg = /^[1][3456789][0-9]{9}$/;
        // const regex = /^1[3456789]\d{9}$/; // 手机号正则表达式
        if (!uName) {
            _this.showErrorFn("请输入用户名")
            return ;
        }

       if (!reg.test(phone)) {
            _this.showErrorFn("请输入正确手机号", "none")
            return ;
        }
        if (region.length == 0) {
            _this.showErrorFn("请选择地区")
            return ;
        }

        if (!detailAddress) {
            _this.showErrorFn("请输入详细地址")
            return ;
        }
        let resiveGoodInfo = {
            uName: uName,
            phone: phone,
            region: region,
            detailAddress: detailAddress
        }
        wx.setStorage({
            key: "resiveGoodInfo",
            data: resiveGoodInfo
        })
        wx.showToast({
            title: "保存成功",
            mask: true
        })
        setTimeout(()=>{
             wx.navigateBack()
        }, 2000)
    }
})